package com.example.translateappjava.Activity.HomeFragmentActivity.Forum;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.translateappjava.R;

public class CreatePostActivity extends AppCompatActivity {

    private EditText etTitle, etContent;
    private TextView tvCategory;
    private Button btnPublish;

    private String selectedCategory = "经验分享"; // 默认分类
    private String[] categories = {"营养食谱", "饮食日记", "健康科普", "经验分享"}; // 健康主题分类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_post);

        setupToolbar();
        initViews();
        setupClickListeners();
    }

    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setTitle("发布帖子");
        }
    }

    private void initViews() {
        etTitle = findViewById(R.id.et_title);
        etContent = findViewById(R.id.et_content);
        tvCategory = findViewById(R.id.tv_category);
        btnPublish = findViewById(R.id.btn_publish);

        tvCategory.setText(selectedCategory);
    }

    private void setupClickListeners() {
        // 分类选择
        tvCategory.setOnClickListener(v -> showCategoryDialog());

        // 发布按钮
        btnPublish.setOnClickListener(v -> publishPost());
    }

    private void showCategoryDialog() {
        androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(this);
        builder.setTitle("选择分类");
        builder.setItems(categories, (dialog, which) -> {
            selectedCategory = categories[which];
            tvCategory.setText(selectedCategory);
        });
        builder.show();
    }

    private void publishPost() {
        String title = etTitle.getText().toString().trim();
        String content = etContent.getText().toString().trim();

        if (TextUtils.isEmpty(title)) {
            Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
            etTitle.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(content)) {
            Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
            etContent.requestFocus();
            return;
        }

        if (title.length() > 100) {
            Toast.makeText(this, "标题不能超过100个字符", Toast.LENGTH_SHORT).show();
            return;
        }

        if (content.length() > 5000) {
            Toast.makeText(this, "内容不能超过5000个字符", Toast.LENGTH_SHORT).show();
            return;
        }

        // 返回结果给ForumActivity
        Intent resultIntent = new Intent();
        resultIntent.putExtra("title", title);
        resultIntent.putExtra("content", content);
        resultIntent.putExtra("category", selectedCategory);
        setResult(RESULT_OK, resultIntent);

        Toast.makeText(this, "帖子发布成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }
}